class Api::V1::ExpressCompaniesController < Api::V1::BaseController
  before_action :authenticate_request!

  def index
    @express_companies = apply_scopes(ExpressCompany).all
    @express_companies = paginate(@express_companies)
    render json: @express_companies, meta: paginate_meta(@express_companies)
  end

  def create
    begin
      @express_company = ExpressCompany.new(create_params)
      authorize @express_company
      @express_company.save!
      render json: @express_company, status: 201
    rescue ActiveRecord::StatementInvalid
      @express_company.errors.add(:exec_error, 'Contact Administrator')
      error_render(@express_company, 403)
    rescue => err
      error_render(err.record, 403)
    end
  end

  def show
    @express_company = ExpressCompany.find(params[:id])
    render json: @express_company, status: 200
  end

  def update
    begin
      @express_company = ExpressCompany.find(params[:id])
      @express_company.update_attributes!(create_params)
      render json: @express_company, status: 200
    rescue => err
      error_render(err.record, 403)
    end
  end

  def destroy
    begin
      @express_company = ExpressCompany.find(params[:id])
      @express_company.destroy!
      render status: 204
    rescue ActiveRecord::RecordNotFound
      render status: 404
    rescue => err
      error_render(err.record, 403)
    end
  end

  private
  def create_params
    params_parse(params)
  end
end
